import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_tutorial/app/modules/state/dependency_binding/counter_controller.dart';

class DependencyBindingPage extends GetView {

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(title: Text('DependencyBindingPage')),

      body: SafeArea(
        child: Column(
          children: [
            GetX<CounterController>(
              init: Get.find<CounterController>(),
              initState: (state) {
                state.controller?.x.value = 26;
              },
              builder: (controller) {
                print('x rebuild');
                return Text(
                  'X: '+controller.x.value.toString(),
                  style: Theme.of(context).textTheme.headline4
                );
              }
            ),
            GetX<CounterController>(
              // init: CounterController(), // 第二次调用 GetX 时，对象已经初始化
              builder: (controller) {
                print('y rebuild');
                return Text(
                  'Y: '+controller.y.value.toString(),
                  style: Theme.of(context).textTheme.headline4
                );
              }
            ),

            GetX<CounterController>(
              builder: (_) {
                print('sum rebuild');
                return Column(
                  children: [
                    Text(
                      'X+Y: '+_.sum.toString(),
                      style: Theme.of(context).textTheme.headline4
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            _.incrementX();
                          }, 
                          child: Text('X +')
                        ),
                        SizedBox(width: 10,),
                        ElevatedButton(
                          onPressed: () {
                            _.incrementY();
                          }, 
                          child: Text('Y +')
                        ),
                      ],
                    )
                  ],
                );
                
              }
            ),
          ],
        )
      )
    );
  }
}